// src/pages/sampling/InspectionLevel.vue
<template>
	<div class="inspection-level">
		<el-card>
			<template #header>
				<div class="header-bar">
					<span>检验水平设置</span>
					<el-button type="primary" size="small" @click="openDialog('add')">新增检验水平</el-button>
				</div>
			</template>

			<el-table :data="levelList" border stripe>
				<el-table-column prop="level" label="水平等级" width="140" />
				<el-table-column prop="description" label="描述说明" />
				<el-table-column prop="type" label="适用类型" width="120" />
				<el-table-column label="操作" width="180">
					<template #default="scope">
						<el-button size="small" @click="openDialog('edit', scope.row)">编辑</el-button>
						<el-button size="small" type="danger" @click="deleteRow(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>

		<el-dialog :title="dialogTitle" v-model="dialogVisible" width="500px">
			<el-form :model="form" label-width="100px">
				<el-form-item label="水平等级">
					<el-input v-model="form.level" placeholder="如 一般 II、S2 等" />
				</el-form-item>
				<el-form-item label="描述说明">
					<el-input v-model="form.description" />
				</el-form-item>
				<el-form-item label="适用类型">
					<el-select v-model="form.type">
						<el-option label="来料" value="来料" />
						<el-option label="过程" value="过程" />
						<el-option label="出货" value="出货" />
					</el-select>
				</el-form-item>
			</el-form>
			<template #footer>
				<el-button @click="dialogVisible = false">取消</el-button>
				<el-button type="primary" @click="submitForm">保存</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script>
import { ref } from 'vue'

export default {
	setup() {
		const levelList = ref([
			{ level: '一般 I', description: '一般检验 水准 I', type: '来料' },
			{ level: '一般 II', description: '常用默认水准', type: '来料' },
			{ level: '一般 III', description: '严格抽检水平', type: '过程' },
			{ level: 'S1', description: '特殊水准 S1', type: '过程' },
			{ level: 'S2', description: '特殊水准 S2', type: '出货' },
			{ level: 'S3', description: '特殊水准 S3', type: '出货' },
			{ level: 'S4', description: '特殊水准 S4', type: '出货' },
			{ level: '加强 I', description: '加严检测用', type: '出货' },
			{ level: '降低 I', description: '放宽检测要求', type: '来料' },
			{ level: '特殊 X', description: '客户指定特殊标准', type: '过程' }
		])

		const dialogVisible = ref(false)
		const dialogTitle = ref('')
		const form = ref({})
		const currentIndex = ref(-1)

		const openDialog = (type, row) => {
			dialogTitle.value = type === 'add' ? '新增检验水平' : '编辑检验水平'
			dialogVisible.value = true
			if (type === 'edit') {
				form.value = { ...row }
				currentIndex.value = levelList.value.findIndex(l => l.level === row.level)
			} else {
				form.value = { level: '', description: '', type: '' }
				currentIndex.value = -1
			}
		}

		const submitForm = () => {
			if (currentIndex.value >= 0) {
				levelList.value[currentIndex.value] = { ...form.value }
			} else {
				levelList.value.push({ ...form.value })
			}
			dialogVisible.value = false
		}

		const deleteRow = (row) => {
			levelList.value = levelList.value.filter(l => l !== row)
		}

		return {
			levelList,
			dialogVisible,
			dialogTitle,
			form,
			openDialog,
			submitForm,
			deleteRow
		}
	}
}
</script>

<style scoped>
.inspection-level {
	padding: 20px;
}

.header-bar {
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-weight: bold;
	font-size: 16px;
}
</style>
